package com.cspg.design.request;

import com.cspg.design.bean.deviceBean.HotLineWorkPlanBean;
import com.cspg.design.common.Constant;
import com.cspg.design.dto.ProjectProcessInformation;
import com.cspg.design.dto.submit.HotLineWork;
import com.cspg.design.http.IService;
import com.cspg.design.http.RetrofitUtil;
import com.cspg.design.http.RetrofitUtilReview;
import com.cspg.design.model.HotLineWorkModel;
import com.cspg.design.presenter.ProjectPresenter;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Project {

    private final RetrofitUtilReview retrofitUtilReview = new RetrofitUtilReview();
    private final RetrofitUtil retrofitUtil= new RetrofitUtil();
    private Call call;
    private final ProjectPresenter presenter;
    private static final String type = "android";

    public Project(ProjectPresenter presenter) {
        this.presenter = presenter;
    }

    public void  getProjectFlow(String ProjectID) throws Exception
    {
        call = retrofitUtilReview.createReq(IService.class).projectFlow( ProjectID);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.httpRequestFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.httpRequestFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                try {
                    Object obj = response.body();
                    List<ProjectProcessInformation> list = (List<ProjectProcessInformation>) obj;
                    if (obj == null)
                    {
                        presenter.httpRequestFailure(Constant.HTTP_LOGIN_FAILURE);
                        return;
                    }
                    else
                    {
                        presenter.projectFlowHttpRequestSuccess(list);
                    }
                }catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.httpRequestFailure("获取数据失败");
            }
        });
    }

    public void  getHotLineWork(String projectId) throws Exception
    {
        call = retrofitUtil.createReq(IService.class).hotLineWorkGet( 0,100,projectId);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.httpRequestFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.isSuccessful()) {
                    try {
                        Object obj = response.body();
                        HotLineWorkModel hotLineWorkModel = (HotLineWorkModel) obj;
                        if (obj == null) {
                            presenter.httpRequestFailure(Constant.HTTP_LOGIN_FAILURE);
                            return;
                        } else {
                            presenter.hotLineWorkHttpRequestSuccess(hotLineWorkModel.getContent());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.httpRequestFailure("获取数据失败");
            }
        });
    }

    public void  updateHotLineWork(HotLineWorkPlanBean hotLineWorkPlanBean) throws Exception
    {
        call = retrofitUtil.createReq(IService.class).hotLineWorkUpdate( hotLineWorkPlanBean);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.httpRequestFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.hotLineWorkUpdateSuccess();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.httpRequestFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                try {
                    Object obj = response.body();
                    if (obj == null)
                    {
                        presenter.httpRequestFailure(Constant.HTTP_LOGIN_FAILURE);
                        return;
                    }
                }catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.httpRequestFailure("获取数据失败");
            }
        });
    }

    public void  addHotLineWork(HotLineWorkPlanBean hotLineWorkPlanBean) throws Exception
    {
        call = retrofitUtil.createReq(IService.class).hotLineWorkAdd( hotLineWorkPlanBean);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.httpRequestFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.hotLineWorkUpdateSuccess();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.httpRequestFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                try {
                    Object obj = response.body();
                    if (obj == null)
                    {
                        presenter.httpRequestFailure(Constant.HTTP_LOGIN_FAILURE);
                        return;
                    }
                    else
                    {
                        presenter.hotLineWorkUpdateSuccess();
                    }
                }catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.httpRequestFailure("获取数据失败");
            }
        });
    }
    public void  insertHotLineWork(HotLineWorkPlanBean hotLineWorkPlanBean) throws Exception
    {
        call = retrofitUtil.createReq(IService.class).hotLineWorkAdd( hotLineWorkPlanBean);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.httpRequestFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.httpRequestFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                try {
                    Object obj = response.body();
                    List<HotLineWorkPlanBean> list = (List<HotLineWorkPlanBean>) obj;
                    if (obj == null)
                    {
                        presenter.httpRequestFailure(Constant.HTTP_LOGIN_FAILURE);
                        return;
                    }
                    else
                    {
                        presenter.hotLineWorkHttpRequestSuccess(list);
                    }
                }catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.httpRequestFailure("获取数据失败");
            }
        });
    }
}
